﻿Public Class frmSwitchProperties
    Public Switch As PurpleKnife.Library.pkSwitch

    Private Sub cmdCancel_Click(sender As System.Object, e As System.EventArgs) Handles cmdCancel.Click
        Me.Hide()
    End Sub

    Private Sub cmdOK_Click(sender As System.Object, e As System.EventArgs) Handles cmdOK.Click
        SanitizeInput()
        If ValidateInput() Then
            Switch.SSHConnectionTimeout = TimeSpan.FromSeconds(numTimeoutConnection.Value)
            Switch.SSHCommandTimeout = TimeSpan.FromSeconds(numTimeoutCommand.Value)
            Switch.DisplayName = txtDisplayName.Text
            Switch.SSHUserName = txtUserName.Text
            Switch.SSHPort = Integer.Parse(txtPort.Text)
            Switch.SSHHost = txtIPAddressHostname.Text
            Switch.SSHPassword = txtPassword.Text
            Me.Hide()
        End If
    End Sub

    Private Sub SanitizeInput()
        txtDisplayName.Text = txtDisplayName.Text.Trim
        txtIPAddressHostname.Text = txtIPAddressHostname.Text.Trim
        txtPort.Text = txtPort.Text.Trim
        txtPassword.Text = txtPassword.Text.Trim
        txtUserName.Text = txtUserName.Text.Trim
    End Sub


    Private Function ValidateInput() As Boolean
        If txtDisplayName.Text = "" Then
            InvMsg("Display Name cannot be empty.")
            txtDisplayName.Focus()
            Return False
        End If
        If txtIPAddressHostname.Text = "" Then
            InvMsg("You must enter an IP or a hostname.")
            txtIPAddressHostname.Focus()
            Return False
        End If
        If txtPort.Text = "" Or (Not IsNumeric(txtPort.Text)) Or Integer.Parse(txtPort.Text) > 65535 Or Integer.Parse(txtPort.Text) < 1 Then
            InvMsg("You must enter a valid port.")
            txtPort.Focus()
            Return False
        End If
        If txtUserName.Text = "" Then
            InvMsg("You must enter an SSH user name.")
            txtUserName.Focus()
            Return False
        End If
        Return True
    End Function

    Private Sub InvMsg(Message As String)
        MsgBox(Message, MsgBoxStyle.Exclamation Or MsgBoxStyle.OkOnly, "PurpleKnife")
    End Sub
End Class